import { createRouter, createWebHashHistory } from 'vue-router'
import Index  from '@/views/teacher/IndexView.vue'
import Login  from '@/views/LoginView.vue'
import Student    from '@/views/student/StuView.vue'
import Class  from '@/views/teacher/ClassView.vue'
import Detail from '@/views/teacher/ClassDetail.vue'
import Question from '@/views/teacher/QuestionView.vue'
import Major from '@/views/teacher/MajorView.vue'
import Book from '@/views/teacher/BookView.vue'
import Admin from '@/views/admin/AdminClassView.vue'
import Resource from '@/views/admin/ResourceView.vue'
import ResourceCate from '@/views/admin/ResourceCateView.vue'
import ResourcePubView from '@/views/admin/ResourcePubView.vue'
import TextBook from '@/views/admin/TextBookView.vue'
import AdminClass from '@/views/admin/ClassView.vue'  
import AdminQuestion from '@/views/admin/QuestionView.vue'
import SystemView from '@/views/admin/SystemView.vue'
import GradeView from '@/views/admin/GradeView.vue'
import ClassesView from '@/views/admin/ClassesView.vue'
import Test from '@/views/TestView.vue'
import AppLayout from '@/views/AppLayout.vue'
import AdminAppLayout from '@/views/AdminAppLayout.vue'

const routes = [
    {
        path: '/ceshi',
        name: 'ceshi',
        component: Test,
        props: true
    },
    {
        path: '/login',
        name: 'login',
        component: Login,
        props: true
    },
    {
        path: '/403',
        name: 'Forbidden',
        component: () => import('@/views/ForbiddenPage.vue')
    },
    {
        path: '/student',
        name: 'student',
        component: Student,
        props: true
    }, 
    {
        path: '/teacher',
        component: AppLayout,
        children:[
            {
                path: '',
                name: 'home',
                component: Index,
                props: true
            },
            {
                path: '/class',
                name: 'class',
                component: Class,
                props: true
            },
            {
                path: '/detail',
                name: 'detail',
                component: Detail,
                props: true
            },
            {
                path: '/book',
                name: 'book',
                component: Book,
                props: true
            },            
            {
                path: '/question',
                name: 'question',
                component: Question,
                props: true
            },
            {
                path: '/major',
                name: 'major',
                component: Major,
                props: true
            },
        ]
    },
    {
        path: '/admin',
        component: AdminAppLayout,
        children:[
            {
                path: '',
                name: 'admin',
                component: Admin,
                props: true
            },
            {
                path: '/user',
                name: 'user',
                component: SystemView,
                props: true
            },
            {
                path: '/textbook',
                name: 'textbook',
                component: TextBook,
                props: true
            },
            {
                path: '/adminclass',
                name: 'adminclass',
                component: AdminClass,
                props: true
            },
            {
                path: '/grade',
                name: 'grade',
                component: GradeView,
                props: true
            },
            {
                path: '/classes',
                name: 'classes',
                component: ClassesView,
                props: true
            },
            {
                path: '/adminquestion',
                name: 'adminquestion',
                component: AdminQuestion,
                props: true
            },
            {
                path: '/resource',
                name: 'resource',
                component: Resource,
                props: true
            },
            {
                path: '/resourcecate',
                name: 'resourcecate',
                component: ResourceCate,
                props: true
            },
            {
                path: '/publish',
                name: 'publish',
                component: ResourcePubView,
                props: true
            },
        ]
    },   
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})


router.beforeEach((to, from, next) => {
    const token = localStorage.getItem("token");
    const role = localStorage.getItem("role");
    const parsedRole = role ? JSON.parse(role) : null;
    const roleKey = parsedRole ? parsedRole.roleKey : null;
    console.log("Role from localStorage:", roleKey); 
    console.log("Role from localStorage:", parsedRole); 

    if (to.path === '/login' || to.path === '/403') {
        return next();
    }

    if (!token) {
        return next('/login');
    }
    if (to.path.startsWith('/admin') && roleKey !== 'admin') {
        return next('/403');
    }

    if (to.path.startsWith('/teacher') && roleKey !== 'teacher') {
        return next('/403');
    }

    if (to.path.startsWith('/student') && roleKey !== 'student') {
        return next('/403');
    }

    next();
});
export default router
